package com.liws.helloeasyexcel.controller;

import com.liws.helloeasyexcel.commons.BaseResult;
import com.liws.helloeasyexcel.listener.StudentEventListener;
import com.liws.helloeasyexcel.model.Student;

import com.liws.helloeasyexcel.service.StudentService;
import com.liws.helloeasyexcel.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: liweisheng
 * @Dcscription: 导入
 * @Date: 2021/12/8 16:20
 */
@RestController
@RequestMapping("/excel")
public class Excelcontroller {

    @Autowired
    StudentService studentService;

    @RequestMapping(value = "/downLoad",method = RequestMethod.POST)
    public BaseResult downLoad(HttpServletResponse response)  {
        String fileName="单体";
        String sheetName="单体sheet";
        ExcelUtil.export(response,fileName,sheetName,studentService.queryStudents(),Student.class);

        return BaseResult.success("导出成功");
    }

    @RequestMapping(value = "/upLoad",method = RequestMethod.POST)
    public BaseResult upLoad(@RequestParam("file") MultipartFile file) {
        List<Student> students = ExcelUtil.importExcel(file, Student.class, new StudentEventListener());

        if (students==null){
            return BaseResult.fail("导入失败");
        }

        return BaseResult.success("导入成功");
    }


}
